import Dialog from '@vant/weapp/dialog/dialog';
import { menu } from './menu'
Page({
  data: {
    menuList: menu,
    secondaryMenu: [],
    showsecondaryMenu: false
  },
  navtoPages (e) {
    let { code } = e.currentTarget.dataset;
    let itemData = {};
    if (this.data.showsecondaryMenu) {
      for (let item of this.data.secondaryMenu) {
        if (item.name == code) {
          itemData = item
        }
      }
    } else {
      for (let item of this.data.menuList) {
        if (item.name == code) {
          itemData = item
        }
      }
    }
    if (Object.keys(itemData).length > 0) {
      let role_id = wx.getStorageSync('role_id');
      if (itemData.jurisdiction.indexOf(role_id) == '-1') return wx.$toast('您没有使用该菜单的权限')
      wx.navigateTo({
        url: itemData.code,
      })
    }
  },
  handleRelieve () {
    Dialog.confirm({
      title: '温馨提示',
      message: '是否确认解除与微信绑定？',
    }).then(() => {
      wx.$post(1, 'WXMiniProgram/update_miniprogram_unbind').then(res => {
        if (res.errcode !== 0) return
        wx.$toast(res.response.errmsg, 1500)
      })
    }).catch(() => {
      // on cancel
    });
  },
  handleQuit () {
    Dialog.confirm({
      title: '温馨提示',
      message: '是否确认退出该账号？',
    }).then(() => {
      let rlatKey = ['refresh', 'token', 'company_name', 'company_id', 'user_name', 'user_id', 'role_id', 'is_platfrom', 'is_agent', 'userLevel',]
      rlatKey.forEach(key => wx.removeStorageSync(key))
      wx.reLaunch({
        url: '/repairPages/service_login/service_login'
      })
    }).catch(() => {
      // on cancel
    });
  },
  onShow () {
    wx.hideHomeButton()
    let perInfo = {
      company_name: wx.getStorageSync('company_name'),
      userName: wx.getStorageSync('user_name'),
      gs_name: wx.getStorageSync('gs_name'),
      role_name: wx.getStorageSync('role_name'),
    }
    this.setData({ perInfo })
  },
  handreturnOnceMenu () {
    // 返回一级菜单
    this.setData({ showsecondaryMenu: !this.data.showsecondaryMenu })
  },
  showSecondaryMenu (e) {
    // 展开二级菜单
    let { code } = e.currentTarget.dataset;
    this.reduceItem(code, itemData => {
      let secondaryMenu = itemData.secondaryMenu
      if (secondaryMenu.length == 0) return wx.$toast('该菜单暂无二级菜单');
      this.setData({ secondaryMenu, showsecondaryMenu: true })
    })
  },
  reduceItem (code, cb) {
    let itemData = {};
    for (let item of this.data.menuList) {
      if (item.name == code) {
        itemData = item
      }
    }
    if (Object.keys(itemData).length > 0) {
      let role_id = wx.getStorageSync('role_id');
      if (itemData.jurisdiction.indexOf(role_id) == '-1') return wx.$toast('您没有使用该菜单的权限')
      cb(itemData)
    }
  }
})